/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package utils;

/**
 *
 * @author Felipe Gutierrez
 */
import java.util.ArrayList;
import java.util.List;

import net.java.dev.greenbox.base.Attribute;
import net.java.dev.greenbox.base.Clazz;
import net.java.dev.greenbox.base.TagValue;

import xmibase.XmiAttribute;

public class FormatationUtils {

	public static final String lowerFirst(String aux) {

		if (aux == null || aux == "") {
			return "";
		} else {
			StringBuilder strBuilder = new StringBuilder(aux);

			strBuilder
					.setCharAt(0, Character.toLowerCase(strBuilder.charAt(0)));

			return strBuilder.toString();
		}

	}

	public static final String upperFirst(String aux) {

		if (aux == null || aux == "") {
			return "";
		} else {
			StringBuilder strBuilder = new StringBuilder(aux);

			strBuilder
					.setCharAt(0, Character.toUpperCase(strBuilder.charAt(0)));

			return strBuilder.toString();
		}

	}

	public String getAttributeType(XmiAttribute attribute) {
		if (attribute.getDataType() == null) {
			return "";
		} else {
			if (attribute.getUpperMultiplicity() == -1) {
				return "List<" + convertPrimitiveType(attribute.getDataType())
						+ ">";
			} else {
				return convertPrimitiveType(attribute.getDataType());
			}
		}

	}

	public String convertPrimitiveType(String type) {

		if (type.compareTo("byte") == 0)
			return "Byte";
		else if (type.compareTo("short") == 0)
			return "Short";
		else if (type.compareTo("int") == 0)
			return "Integer";
		else if (type.compareTo("long") == 0)
			return "Long";
		else if (type.compareTo("char") == 0)
			return "Character";
		else if (type.compareTo("boolean") == 0)
			return "Boolean";
		else if (type.compareTo("float") == 0)
			return "Float";
		else if (type.compareTo("double") == 0)
			return "Double";
		else if (type.compareTo("Date") == 0)
			return "Long";
		else if (type.compareTo("ResourceDescriptor") == 0)
			return "String";
		else
			return type;

	}

	public Boolean hasValidationRules(Attribute attribute) {
		for (TagValue tagValue : attribute.getTagsValue()) {
			if (tagValue.getId().trim().equalsIgnoreCase("validator")) {
				return true;
			}
		}

		return false;
	}

	public List<TagValue> getValidationRules(Attribute attribute) {
		List<TagValue> validatioRules = new ArrayList<TagValue>();

		for (TagValue tagValue : attribute.getTagsValue()) {
			if (tagValue.getId().trim().equalsIgnoreCase("validator")) {
				validatioRules.add(tagValue);
			}
		}

		return validatioRules;

	}

	public String getBOClassGeneralization(Clazz classe) {
		if (classe.getHasSuperClass()) {
			return "extends " + classe.getSuperClazz().getClassName() + "BO";
		} else {
			return "extends GenericBOImpl";
		}
	}

	public Boolean isStateMachine(Clazz classe) {

		for (Clazz tmpInterface : classe.getInterfacesClazz()) {
			if (tmpInterface.getClassName().trim().equalsIgnoreCase(
					"StateMachineEntity")) {
				return true;
			} else {
				return false;
			}
		}
		return false;
	}

	public Boolean isGenericEntity(Clazz classe) {
		for (Clazz tmpInterface : classe.getInterfacesClazz()) {
			if (tmpInterface.getClassName().trim().equalsIgnoreCase(
					"GenericEntity")
					|| tmpInterface.getClassName().trim().equalsIgnoreCase(
							"StateMachineEntity")) {
				return true;
			} else {
				return false;
			}
		}
		return false;
	}

}
